/*
* Copyright (c) 2009, Active Web Solutions Limited
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3- The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY Active Web Solutions Limited ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Active Web Solutions Limited BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.aws.jazure;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Objects of this class represent entities store in a table in the table storage.  
 *  
 * The properties are stored as Map with String keys representing the property names and values 
 * as instances of <code>TableStorageProperty</code> holding the type and the value.
 * @author dyaa.albakour
 *
 */
public class TableStorageEntity { 
	
	protected Map<String,TableStorageProperty> properties = new HashMap<String, TableStorageProperty>();
	
	// why do we need separate entities
//	protected String partitionKey;
	
//	protected String rowKey;
	
//	protected Calendar timeStamp = Calendar.getInstance();

	public TableStorageEntity(){
		super();
		this.getProperties().put(TableStorageProperty.PartitionKey,null );
		this.getProperties().put(TableStorageProperty.RowKey,null);
		this.getProperties().put(TableStorageProperty.Timestamp,
					new TableStorageProperty( TableStorageProperty.EdmDateTime,Calendar.getInstance() ) );	
	}
	
	public TableStorageEntity(String partitionKey,
			String rowKey) {
		super();
		this.getProperties().put( TableStorageProperty.PartitionKey,
					new TableStorageProperty(TableStorageProperty.EdmString,partitionKey) );
		this.getProperties().put(TableStorageProperty.RowKey,
					new TableStorageProperty(TableStorageProperty.EdmString,rowKey));
		this.getProperties().put(TableStorageProperty.Timestamp,
					new TableStorageProperty(TableStorageProperty.EdmDateTime,Calendar.getInstance()));
	}

	public Calendar getTimeStamp() {
		return (Calendar) getProperties().get(TableStorageProperty.Timestamp).getValue();
	}

	
	public String getPartitionKey() {
		return (String) getProperties().get(TableStorageProperty.PartitionKey).getValue();
	}

	public void setPartitionKey(String partitionKey) {
		this.getProperties().put(TableStorageProperty.PartitionKey,
				new TableStorageProperty(TableStorageProperty.EdmString,partitionKey) );
	}

	public String getRowKey() {
		return (String) getProperties().get(TableStorageProperty.RowKey).getValue();
	}

	public void setRowKey(String rowKey) {
		this.getProperties().put( TableStorageProperty.RowKey,
				new TableStorageProperty(TableStorageProperty.EdmString,rowKey) );
	}

	public Map<String, TableStorageProperty> getProperties() {
		return properties;
	}
	
	public String toString(){
		StringBuilder strbuilder = new StringBuilder();
		strbuilder.append("Table Storage Entity: ["+ getPartitionKey()+":"+getRowKey()+"]\n" );
		
		for(String pro :getProperties().keySet() ){			
			strbuilder.append(pro +"(type: " + getProperties().get(pro).getType() +
								   ", value: " + getProperties().get(pro).getValue() +")\n" );
		}		
		return strbuilder.toString();
	}
}
